package com.gcs.application.gui.views;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.ViewPart;

import com.gcs.application.gui.actions.ClearLogAction;

public class LogView extends ViewPart {
	// Declare the controls for this view
	public static final String ID = LogView.class.getName();
	
	// Declare the controls of view
	private FormToolkit toolkit;	
	private ScrolledForm scrolledForm;
	
	private final int numCols = 1;
	
	public static StyledText textLog;	
	private Action actionClearLog;
		
	@Override
	public void createPartControl(Composite parent) {
		// Create the form toolkit
		this.toolkit = new FormToolkit(parent.getDisplay()); 
		
		// Create the form scrolled and set layout
		this.scrolledForm = this.toolkit.createScrolledForm(parent);
		GridLayout layout = new GridLayout(this.numCols, true);
		this.scrolledForm.getBody().setLayout(layout);		
		
		// Create the text area into the scrolled form		
		textLog = new StyledText(this.scrolledForm.getBody(), 
				SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		textLog.setLayoutData(new GridData(GridData.FILL_BOTH));
		textLog.setEnabled(true);
		textLog.addListener(SWT.Modify, new Listener(){
		    public void handleEvent(Event e){
		    	textLog.setTopIndex(textLog.getLineCount() - 1);
		    }
		});
		
		this.actionClearLog = new ClearLogAction();
		this.getViewSite().getActionBars().getToolBarManager().add(this.actionClearLog);
	}

	@Override
	public void setFocus() {
		this.scrolledForm.setFocus();
	}
}
